<?php
/**
* @file index.php
* Copyright (c) Friends Group. All rights reserved.
* $Id$
*
*/

require_once(realpath(dirname(__FILE__)) . '/../config.php');
require_once(realpath(dirname(__FILE__)) . '/../includes/common/ServiceFactory.php');

Session::start();

$commonService = ServiceFactory::get();
$userService = ServiceFactory::get("UserService");
$authService = ServiceFactory::get("AuthenticationService");

if(!$authService -> isAuthenticated())
    Response::redirect(Response::addQueryArg(array("back" => $_SERVER['REQUEST_URI']), "./login.php"));

$currentFile = 'index.php';
$user = $authService -> getCurrentUser();

include("admin-header.php");
include("menu-bar.php");

if(!$authService -> isUser($user)) 
{
?>
<h2>Các tin nhắn hệ thống</h2>
<table class="TableWrapper" width="100%">
    <colgroup>
        <col width="20%">
        <col width="45%">
        <col width="20%">
        <col width="15%">
    </colgroup>
    <thead>
        <tr>
            <th>Type</th>
            <th>Message</th>
            <th>Receiver</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody id="systemMessage">
        <?php 
        $class = "";
        $messages = ServiceFactory::get("MessageService") -> getSystemMessages();
        if(count($messages))
        {
            foreach ($messages as $ms)
            {
                $receiver = $userService -> findUserById($ms -> getAccountID());
                echo "<tr class='{$ms -> getMessageType()}'>";
                echo "<td class='{$ms -> getMessageType()}'>{$ms -> getMessageType()}</th>";
                echo "<td>{$ms -> getMessages()}</td>";
                echo "<td>{$receiver -> getNickname()}</td>";
                $rt = DateUtility::friendlyDate($ms -> getPostTime());
                echo "<td>{$rt}</td>";
                echo "</tr>";
            }
        }
        ?>
    </tbody>
</table>

<?php 
}
else
    Response::redirect("./profile.php");
include("admin-footer.php");

?>